﻿/*
 * @author: wizardc
 */

using UnityEngine;

namespace Dou.UI
{
    /// <summary>
    /// 刘海屏适配器
    /// 将该脚本添加到需要支持刘海屏的UI的根节点即可，注意UI内部的布局必须都是相对布局。
    /// </summary>
    [ExecuteInEditMode]
    public class NotchAdaptor : MonoBehaviour
    {
        public Vector2 min;
        public Vector2 max;

        private RectTransform _rectTransform;

        private void Start()
        {
            _rectTransform = GetComponent<RectTransform>();
            Adjust();
        }

        private void Adjust()
        {
            _rectTransform.anchorMin = new Vector2(min.x / Screen.width, min.y / Screen.height);
            _rectTransform.anchorMax = new Vector2(1 - max.x / Screen.width, 1 - max.y / Screen.height);
        }

#if UNITY_EDITOR
        private void LateUpdate()
        {
            Adjust();
        }
#endif
    }
}
